<?php
declare(strict_types=1);

namespace lib\data\virtual;

/**
 * 虚拟数据类, 用于同步数据 数据库=>客户端
 */
class Messages extends \lib\sync\SyncableData {
    /**
     * 从数据库中获取数据
     */
    
    protected static string $SyncKey = 'Messages';
    protected static string $TableName = 'tp_nb_goods_messages';

    public static function remove(int $mid, int $msgId):array {
        $db = new \sys\Db();
        $syncObject = new \lib\sync\Synchronize($mid, $db);
        if(1 == $db->table(static::$TableName)->where([['mid', '=', $mid], ['id','=', $msgId]])->delete()){
            $syncObject->rm(static::$SyncKey, $msgId);
            return ['success'=>true, 'message'=>'删除成功'];
        }
        return ['success'=>false, 'message'=>'删除失败'];
    }

    public static function readed(int $mid, int $msgId):array {
        $db = new \sys\Db();
        $syncObject = new \lib\sync\Synchronize($mid, $db);
        if(1 == $db->table(static::$TableName)->where([['mid', '=', $mid], ['id','=', $msgId]])->update(['readed'=>1])){
            $syncObject->up(static::$SyncKey, $msgId, ['readed'=>1]);
            return ['success'=>true, 'message'=>'标记成功'];
        }
        return ['success'=>false, 'message'=>'标记失败'];
    }

    static protected function buildSyncSelect(\sys\Db $db, int $mid, int $time, int $lastId = 0) :\sys\SqlBuilder {
        $time = time() - 86400 * 7;
        return parent::buildSyncSelect($db, $mid, $time, $lastId);
    }
}